package com.woniuxy.comsumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.woniuxy.commons.entity.Goods;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OderController {
    @Resource
    private RestTemplate restTemplate;
    String url = "http://PROVIDER/goods/";

    @HystrixCommand(fallbackMethod = "abc")  //熔断器使用注解
    @RequestMapping("/find")
    public List<Goods> find(){
        //发送请求
        List forObject = restTemplate.getForObject(url+"all", List.class);
        return forObject;
    }
    public List<Goods> abc(){
        //发送请求
        List<Goods> forObject = new ArrayList<>();
        return forObject;
    }


    @GetMapping("/find1")
    public Goods fiind1(){
        Goods goods = restTemplate.getForObject(url + "find/1004", Goods.class);
        return goods;
    }

    @GetMapping("/add")
    public boolean add(){
        Goods goods = new Goods(1005, "内衣", 300);
        Boolean aBoolean = restTemplate.postForObject(url + "add", goods, boolean.class);
        return aBoolean;
    }

    @GetMapping("/update")
    public boolean update(){
        Goods goods = new Goods(1005, "内衣", 300);
        restTemplate.put(url + "update", goods);
        return true;
    }
}
